﻿using System;
using System.Data;
using Dng.DataFactory;
using Dng.Utilities;
using ShippingManage.DataAccessor.Entities;

namespace ShippingManage.Models
{
	public enum DataTypes
	{
		User = 0,
		HAWB = 1,
		VienDongShipping = 2
	}

	public class VersionInfoModel
	{
		public VersionInfo Get(DataTypes dataType, Guid id)
		{
			VersionInfo result = null;
			ProviderFactory dataPF = new ProviderFactory();
			dataPF.OpenConnection();
			try
			{
				result = Get(dataPF, dataType, id);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				dataPF.CloseConnection();
				dataPF.Dispose();
				dataPF = null;
			}
			return result;
		}

		public VersionInfo Get(ProviderFactory dataPF, DataTypes dataType, Guid id)
		{
			var cmd = dataPF.CreateCommand("SELECT * FROM VersionInfoes WHERE DataType=@DataType ItemId=@ItemId");
			cmd.Parameters.Add(dataPF.CreateParameter("@ItemId", DbType.StringFixedLength, Services.I.G2S(id)));
			cmd.Parameters.Add(dataPF.CreateParameter("@DataType", DbType.Int16, (short)dataType));
			return dataPF.FillSingle<VersionInfo>(cmd.ExecuteReader());
		}

		#region Save information into database
		// Save object data into database
		public void Save(VersionInfo obj)
		{
			ProviderFactory dataPF = new ProviderFactory();
			dataPF.OpenConnection();
			try
			{
				Save(dataPF, obj);
			}
			finally
			{
				dataPF.CloseConnection();
				dataPF.Dispose();
				dataPF = null;
			}
		}

		// Save object data into database
		public void Save(ProviderFactory dataPF, VersionInfo obj)
		{

			// TO DO: Your validate code here!

			try
			{
				// TO DO: Check new!
				if (obj.Id == Guid.Empty)
				{
					obj.Id = Guid.NewGuid();
					Add(dataPF, obj);
				}
				else
					Update(dataPF, obj);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		// Add new object data into database
		public void Add(ProviderFactory dataPF, VersionInfo obj)
		{

			// TO DO: Your validate code before adding new here!

			string insertSQL = @"INSERT INTO System.Data.DataRowView (
														Created,
														CreatedId,
														DataType,
														ItemId,
														Version,
														Id
													) VALUES (
														@Created,
														@CreatedId,
														@DataType,
														@ItemId,
														@Version,
														@Id
													)";
			IDbCommand command = dataPF.CreateCommand(insertSQL);

			command.Parameters.Add(dataPF.CreateParameter("@Created", DbType.StringFixedLength, Services.I.DT2S(obj.Created)));
			command.Parameters.Add(dataPF.CreateParameter("@CreatedId", DbType.StringFixedLength, Services.I.G2S(obj.CreatedId)));
			command.Parameters.Add(dataPF.CreateParameter("@DataType", DbType.Int16, obj.DataType));
			command.Parameters.Add(dataPF.CreateParameter("@ItemId", DbType.StringFixedLength, obj.ItemId));
			command.Parameters.Add(dataPF.CreateParameter("@Version", DbType.StringFixedLength, Services.I.DT2S(obj.Version)));
			command.Parameters.Add(dataPF.CreateParameter("@Id", DbType.StringFixedLength, Services.I.G2S(obj.Id)));

			command.ExecuteNonQuery();
		}

		// Update existing object data into database
		public void Update(ProviderFactory dataPF, VersionInfo obj)
		{

			// TO DO: Your validate code before updating here!

			string updateSQL = @"UPDATE System.Data.DataRowView SET 
														Created=@Created,
														CreatedId=@CreatedId,
														Version=@Version,
														Id=@Id
													WHERE DataType=@DataType AND ItemId=@ItemId";
			IDbCommand command = dataPF.CreateCommand(updateSQL);

			command.Parameters.Add(dataPF.CreateParameter("@Created", DbType.StringFixedLength, Services.I.DT2S(obj.Created)));
			command.Parameters.Add(dataPF.CreateParameter("@CreatedId", DbType.StringFixedLength, Services.I.G2S(obj.CreatedId)));
			command.Parameters.Add(dataPF.CreateParameter("@DataType", DbType.Int16, obj.DataType));
			command.Parameters.Add(dataPF.CreateParameter("@ItemId", DbType.StringFixedLength, obj.ItemId));
			command.Parameters.Add(dataPF.CreateParameter("@Version", DbType.StringFixedLength, Services.I.DT2S(obj.Version)));
			command.Parameters.Add(dataPF.CreateParameter("@Id", DbType.StringFixedLength, Services.I.G2S(obj.Id)));

			try
			{
				command.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		#endregion

		#region Delete information from database
		// Delete object data from database
		public void Delete(Guid id)
		{
			ProviderFactory dataPF = new ProviderFactory();
			dataPF.OpenConnection();
			try
			{
				Delete(dataPF, id);
			}
			finally
			{
				dataPF.CloseConnection();
				dataPF.Dispose();
				dataPF = null;
			}
		}

		// Delete object data from database
		public void Delete(ProviderFactory dataPF, Guid id)
		{
			OnDelete(dataPF, id);
		}

		protected virtual void OnDelete(ProviderFactory dataPF, Guid id)
		{
			string deleteSQL = @"DELETE FROM VersionInfoes
														WHERE Id=@Id";
			IDbCommand command = dataPF.CreateCommand(deleteSQL);
			command.Parameters.Add(dataPF.CreateParameter("@Id", DbType.StringFixedLength, Services.I.G2S(id)));
			command.ExecuteNonQuery();
		}

		#endregion
	}
}
